package com.midele.goodstuff.util

import android.util.Log

/**
 * 日志工具类
 * 支持配置是否打印日志
 * 支持配置日志级别
 */
object LogUtils {
    // 日志级别枚举
    enum class LogLevel {
        VERBOSE,
        DEBUG,
        INFO,
        WARN,
        ERROR
    }

    // 默认配置
    private var isLogEnabled = true
    private var currentLogLevel = LogLevel.DEBUG
    private const val DEFAULT_TAG = "midele_log"

    /**
     * 配置日志工具
     * @param enabled 是否启用日志
     * @param level 日志级别
     */
    fun config(enabled: Boolean, level: LogLevel) {
        isLogEnabled = enabled
        currentLogLevel = level
    }

    /**
     * 判断是否应该打印日志
     */
    private fun shouldLog(level: LogLevel): Boolean {
        return isLogEnabled && level.ordinal >= currentLogLevel.ordinal
    }

    fun v(tag: String = DEFAULT_TAG, message: String) {
        if (shouldLog(LogLevel.VERBOSE)) {
            Log.v(tag, message)
        }
    }

    fun d(tag: String = DEFAULT_TAG, message: String) {
        if (shouldLog(LogLevel.DEBUG)) {
            Log.d(tag, message)
        }
    }

    fun i(tag: String = DEFAULT_TAG, message: String) {
        if (shouldLog(LogLevel.INFO)) {
            Log.i(tag, message)
        }
    }

    fun w(tag: String = DEFAULT_TAG, message: String) {
        if (shouldLog(LogLevel.WARN)) {
            Log.w(tag, message)
        }
    }

    fun e(tag: String = DEFAULT_TAG, message: String, throwable: Throwable? = null) {
        if (shouldLog(LogLevel.ERROR)) {
            if (throwable != null) {
                Log.e(tag, message, throwable)
            } else {
                Log.e(tag, message)
            }
        }
    }
} 